# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views.decorators.csrf import csrf_exempt

from libs import FileUtil


@csrf_exempt
def addFault(request):
    result = request.POST.get("result","")
    memo = request.POST.get("memo","")
    checktype = request.POST.get("checkType",-1)
    data = dict(status=0,message="保存失败",data=[])
    try:
        if not result:
            data["message"] = "检查结果必须填写"
            raise Exception()
        if checktype == -1:
            data["message"] = "检查类别错误，请重新选择"
            raise Exception()
        # 保存数据库
        data["status"] = 1
        data["message"] = "保存成功"
    except Exception as e:
        pass
    return JsonResponse({"data":data})

@csrf_exempt
def uploadFile(request):
    data = {
        "status": 0,
        "message": u"上传文件失败",
        "data": []
    }
    userdata = []
    try:
        if request.method == "POST":
            myFile = request.FILES.getlist("uploadfile")
            # 上传文件
            data1 = FileUtil.UploadFiles(myFile)
            if data1["status"] == 1:
                data["status"] = 1
                for i in data1["data"]:
                    # 数据打包
                    info = {}
                    info["file_url"] = i["file_url"]
                    userdata.append(info)
            data["message"] = data1["message"]
            data["data"] = userdata
    except:
        data = {
            "status": 0,
            "message": u"上传文件失败",
            "data": []
        }
    return JsonResponse(data)